package main;

import java.util.HashMap;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import dao.DaoException;
import dao.DaoGeneral;
import dao.DaoStyle;
import dao.NotFoundException;

import bean.general.Preference;
import bean.general.Style;

@ManagedBean
@ApplicationScoped
/**
 * Class to load the main info at server start
 * @author mario.lagana
 *
 */
public class CachedInfo {
	private ResourceBundle general;
	private Style styleInUse;
	private HashMap<String, Preference> preferences;

	@ManagedProperty(value="#{daoGeneral}")
	private DaoGeneral daoGeneral;
	public void setDaoGeneral(DaoGeneral daoGeneral) {
		this.daoGeneral = daoGeneral;
	}

	@ManagedProperty(value="#{daoStyle}")
	private DaoStyle daoStyle;
	public void setDaoStyle(DaoStyle daoStyle) {
		this.daoStyle = daoStyle;
	}

	public static String LOG_FILE_DATE_FORMAT = "log_file_date_format";
	public static String LOG_DIR = "log_dir";
	public static String LOG_LEVEL = "log_level";
	public static String LOG_DEBUG_MODE = "log_debug_mode";
	public static String LOG_DATE_FORMAT = "log_date_format";

	public CachedInfo() {
		general = ResourceBundle.getBundle("properties.general");
	}

	@PostConstruct
	public void init() {
		preferences = getPreferencesFromDB();
		styleInUse = getStyleInfoFromDB();
	}

	/**
	 * Get the style stored in the preferences and load its info from database
	 * @return
	 */
	private Style getStyleInfoFromDB() {
		String styleName = preferences.get(Preference.STYLE_IN_USE).getValue();

		try {
			styleInUse = daoStyle.getStyle(styleName);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return styleInUse;
	}

	////////////////////////////////////////////////////////////
	//Preferences management
	/**
	 * Get the preferences from database
	 * @return
	 */
	private HashMap<String, Preference> getPreferencesFromDB() {
		try {
			preferences = daoGeneral.getPreferences();
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return preferences;
	}

	/**
	 * Get a preference by name
	 * @param preferenceName
	 * @return
	 */
	public String getPreferenceValue(String preferenceName){
		Preference temp = preferences.get(preferenceName);
		String value = null;

		if (temp != null)
			value = temp.getValue();

		if (value != null)
			return value;
		else 
			return preferenceName;
	}

	public String getHomeTitle(){
		return getPreferenceValue(Preference.HOME_TITLE);
	}
	public String getHomeHeader(){
		return getPreferenceValue(Preference.HOME_HEADER);
	}
	public String getStyleInUseId(){
		return getPreferenceValue(Preference.STYLE_IN_USE);
	}
	public String getSiteName(){
		return getPreferenceValue(Preference.SITE_NAME);
	}
	////////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////////
	//Getters and setters
	public ResourceBundle getGeneral() {
		return general;
	}
	public void setGeneral(ResourceBundle general) {
		this.general = general;
	}
	public HashMap<String, Preference> getPreferences() {
		return preferences;
	}	
	public void setPreferences(HashMap<String, Preference> preferences) {
		this.preferences = preferences;
	}
	public Style getStyleInUse() {
		return styleInUse;
	}
	public void setStyleInUse(Style styleInUse) {
		this.styleInUse = styleInUse;
	}
	////////////////////////////////////////////////////////////
}
